<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/19
 * Time: 17:39
 */
namespace Admin\Controller;
use Admin\Controller\CommonController;

class PartitionController extends CommonController{
    public function index(){
        //查分区数据赋给模板
        $count      = M("partition")->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev' , '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('first', '第一页');
        $show       = $Page->show();// 分页显示输出
        $partition=M("partition")->order("partition")->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign("page",$show);//把分页数据赋给模板
        $this->assign("partition",$partition);
        $this->display();
    }
    //分区的添加
    public function add(){
        if(!IS_POST) {
            $this->display();
        }else{
            $data=I("post.");
            $test=M("partition")->data($data)->add();
            if($test) {
                $this->success('操作成功！',__MODULE__.'/partition/index');
            } else {
                $this->error('操作失败！',__MODULE__.'/partition/index');
            }
        }

    }
    //分区的修改
    public function edit(){
        if(IS_POST) {
            $id= I("get.nid");
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/partition/index');
            }else{
                $data=I("post.");
                $test=M("partition")->data($data)->where("id={$id}")->save();
                if($test) {
                    $this->success('操作成功！',__MODULE__.'/partition/index');
                }else{
                    $this->error("操作失败！你没有修改数据");
                }
            }
        }else{
            $id=I("get.nid");
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/partition/index');
            }else{
                $arr=M("partition")->where("id={$id}")->select();
                $this->assign("arr",$arr);
                $this->display();
            }
        }
    }
    //分区的删除
    public function del(){
        $nid = intval($_GET['nid']);
        if(empty($nid)) {
            $this->error('异常操作！',__MODULE__.'/partition/index');
        }
        if(M("partition")->where("id = {$nid}")->delete() !== fasle) {
            $this->success('操作成功！',__MODULE__.'/partition/index');
        } else {
            $this->error('操作失败！',__MODULE__.'/partition/index');
        }
    }
    //分区下添加国家
    public function limits() {
        $partition_id = intval($_REQUEST['partition_id']);
        if(IS_POST) {
            $mod = M("partition_country");
            $country_id=$_POST['country_id'];
            $cid=$mod->field("country_id")->where("partition_id={$partition_id}")->select();
            $counid=[];
            foreach($cid as $va){
                $counid[]=$va['country_id'];
            }
            $dc=array_diff($counid,$country_id);
            $ac=array_diff($country_id,$counid);
            if(!empty($dc)){
                foreach($dc as $v){
                    $bool =$mod->where("country_id={$v} and partition_id={$partition_id}")->delete();
                }
            }elseif(!empty($ac)){
                foreach($ac as $val){
                    $bol =$mod->data(array("country_id"=>$val,"partition_id"=>$partition_id))->add();
                }
            }else{
                $this->error("没有勾选国家");
            }
            if($bol||$bool){
                  $this->success("操作成功",__MODULE__.'/partition/index')  ;
            }else{
                $this->error("操作失败");
            }
        } else {
            $country = $this->dg(M("country")->field("id,pid,cname,ename")->order("ename asc")->select());
            $pc = M("partition_country")->field("country_id")->where("partition_id={$partition_id}")->select();
            foreach($pc as $value){
                $pcid[]=$value['country_id'];
            }
            $this->assign("pc",$pcid);
            $this->assign("country",$country);
            $this->assign("partition_id",$partition_id);
            $this->display();
        }
    }
    public function dg($data,$pid=0,$empty="&nbsp;&nbsp;"){
        $arr = [];
        foreach($data as $val){
            if($val['pid']==$pid){
                $val['cname']=$empty.$val['cname'];
                $arr[]=$val;
                foreach($this->dg($data,$val['id'],$empty) as $v){
                    $arr[]=$v;
                }
            }
        }
        return $arr;
    }
    public function _empty(){
        $this->error("404");
    }

}